﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using DL;

public partial class _Default : System.Web.UI.Page
{
    DL.CarService dbconnector = new DL.CarService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void lnkDetails_Click(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            Response.Redirect("/CarList.aspx?CarID=" + e.CommandArgument.ToString());
        }
        if (e.CommandName == "Delete")
        {
            AddTrashed(e.CommandArgument.ToString());

        }
    }
    public void AddTrashed(string carID)
    {
        DL.CarService dbconnector = new DL.CarService();
        int id = Convert.ToInt32(carID);
        dbconnector.AddTrashed(id);
        BindData();

    }

    #region Bind Data
    protected void BindData()
    {
        User user = GetAuthUser();
        int userID = user == null ? 0 : user.UserID;
        repCars.DataSource = dbconnector.GetCarList(userID, null, false).OrderByDescending(o => o.CarID).ToList();
        repCars.DataBind();
        repArchived.DataSource = dbconnector.GetCarList(userID, null, true);
        repArchived.DataBind();
    }
    #endregion

    public User GetAuthUser()
    {
        var user = (Session["AuthUser"] as User);
        return user;
    }
}